cmake_minimum_required(VERSION 3.5)
project(cfgparser_lib VERSION 1.0.0 LANGUAGES C CXX)

# 设置 C/C++ 标准
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 包含目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# 查找所有源文件（C 和 C++）
file(GLOB_RECURSE C_SOURCE_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c
)

file(GLOB_RECURSE CXX_SOURCE_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
)

# 创建静态库
add_library(cfgparser_lib STATIC 
    ${C_SOURCE_FILES}
    ${CXX_SOURCE_FILES}
)

# 设置库的目标属性
set_target_properties(cfgparser_lib PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    POSITION_INDEPENDENT_CODE ON
)

# 导出头文件
target_include_directories(cfgparser_lib PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

# 平台特定的链接库
if(WIN32)
    target_link_libraries(cfgparser_lib PRIVATE shell32)
elseif(UNIX)
    # Unix 系统不需要额外的链接库
endif()

# 编译选项
if(MSVC)
    target_compile_options(cfgparser_lib PRIVATE /W4)
    target_compile_definitions(cfgparser_lib PRIVATE _CRT_SECURE_NO_WARNINGS)
else()
    target_compile_options(cfgparser_lib PRIVATE -Wall -Wextra -pedantic)
endif()

# CLI tool
add_executable(cfgparser_cli
    tools/cfgparser_cli_main.cpp
    tools/cfgparser_cli_app.cpp
)
target_link_libraries(cfgparser_cli PRIVATE cfgparser_lib)
target_include_directories(cfgparser_cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
if(NOT MSVC)
    target_compile_options(cfgparser_cli PRIVATE -Wall -Wextra -pedantic)
endif()

# Tests / demos
if(PREVIZ_BUILD_TESTS AND BUILD_TESTING)
    add_executable(cfgparser_demo tests/cfgparser_demo.cpp)
    target_link_libraries(cfgparser_demo PRIVATE cfgparser_lib)
    add_test(NAME cfgparser_demo COMMAND cfgparser_demo)
endif()

# 安装规则
install(TARGETS cfgparser_lib
    EXPORT cfgparser_lib-targets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
)

install(TARGETS cfgparser_cli
    RUNTIME DESTINATION bin
)

install(DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
)

# 导出配置
install(EXPORT cfgparser_lib-targets
    FILE cfgparser_lib-targets.cmake
    DESTINATION lib/cmake/cfgparser_lib
)